package com.zhoukai.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Description TODO
 * @Author Mr.ZHOU
 * @Date Created in 2021/12/8 14:07
 */
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "https://apis.map.qq.com/ws/location/v1/ip?ip=111.206.145.41&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77";

    public static final String ACCESSKEY="6f89b97ca6cbab4d100dae5d3a969532";

    public static String getRealAddressByIP(String ip) {
        String address = "XX XX";
        // 内网不查询
//        if (IpUtils.internalIp(ip)) {
//            return "内网IP";
//        }
//        String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip+"&key="+ACCESSKEY);
        String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip);
        if (StringUtils.isEmpty(rspStr)) {
            log.error("获取地理位置异常 {}", ip);
            return address;
        }
        System.out.println(rspStr);
        String addres = rspStr;
        //第一个参数为要替换的内容，注意[要转义字符\\，多种用|,第二个参数为替换成的内容
        address = addres.replaceAll("\\[|\\]|,","  ");
        return address;
    }

    public static void main(String[] args) {
        String realAddressByIP = getRealAddressByIP("219.140.226.161");
        System.out.println(realAddressByIP);

    }

}
